﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnContext
    {
        public static class MenuShapes
        {
            public static string MethodID = "";

            public static bool ContextShown = true;

            public static void ContextGUI(int id, Rect selectionRect)
            {
                if (ContextShown || InstanceIDs.Count < 1) return;
                ContextShown = true;
                ShowContextMenu();
            }

            public static void ShowContextMenu()
            {
                ContextShape1 = "";
                var allShapes = ContextUtility.GetAllBlendshapes();

                GenericMenu menu = new GenericMenu();
                var label = ContextShape0 != "" ? ContextShape0 + " + " : "";
                menu.AddDisabledItem(new GUIContent(label));
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Cancel"), false, ContextMenuSelected, "Cancel");
                menu.AddSeparator("");

                foreach (var shape in allShapes)
                {
                    if (shape == ContextShape0) menu.AddDisabledItem(new GUIContent(shape));
                    else menu.AddItem(new GUIContent(shape), false, ContextMenuSelected, shape);
                }

                menu.ShowAsContext();
            }

            private static void ContextMenuSelected(object id)
            {
                var shape = id as string;
                ContextShape1 = string.IsNullOrEmpty(shape) ? "null" : shape;
                InvokeContext(MethodID);
            }
        }

    }
}
